package com.freeter.modules.controller;


import com.freeter.modules.gjs.entity.UsersDivisions;
import com.freeter.modules.gjs.service.UsersDivisionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/users-divisions")
@CrossOrigin(origins = "*")
public class UsersDivisionsController {
    @Autowired
    private UsersDivisionsService usersDivisionsService;

    @GetMapping
    public ResponseEntity<List<UsersDivisions>> getAllUsersDivisions() {
        List<UsersDivisions> usersDivisions = usersDivisionsService.getAllUsersDivisions();
        return ResponseEntity.ok(usersDivisions);
    }

    @GetMapping("/{id}")
    public ResponseEntity<UsersDivisions> getUsersDivisionsById(@PathVariable Long id) {
        UsersDivisions usersDivisions = usersDivisionsService.getUsersDivisionsById(id);
        if (usersDivisions != null) {
            return ResponseEntity.ok(usersDivisions);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<UsersDivisions>> getUsersDivisionsByUserId(@PathVariable Long userId) {
        List<UsersDivisions> usersDivisions = usersDivisionsService.getUsersDivisionsByUserId(userId);
        return ResponseEntity.ok(usersDivisions);
    }

    @GetMapping("/division/{divisionCode}")
    public ResponseEntity<List<UsersDivisions>> getUsersDivisionsByDivisionCode(@PathVariable String divisionCode) {
        List<UsersDivisions> usersDivisions = usersDivisionsService.getUsersDivisionsByDivisionCode(divisionCode);
        return ResponseEntity.ok(usersDivisions);
    }

    @GetMapping("/user/{userId}/division/{divisionCode}")
    public ResponseEntity<UsersDivisions> getUsersDivisionsByUserAndDivision(
            @PathVariable Long userId, @PathVariable String divisionCode) {
        UsersDivisions usersDivisions = usersDivisionsService.getUsersDivisionsByUserAndDivision(userId, divisionCode);
        if (usersDivisions != null) {
            return ResponseEntity.ok(usersDivisions);
        }
        return ResponseEntity.notFound().build();
    }

    @PostMapping
    public ResponseEntity<UsersDivisions> createUsersDivisions(@RequestBody UsersDivisions usersDivisions) {
        UsersDivisions createdUsersDivisions = usersDivisionsService.createUsersDivisions(usersDivisions);
        return ResponseEntity.ok(createdUsersDivisions);
    }

    @PutMapping("/{id}")
    public ResponseEntity<UsersDivisions> updateUsersDivisions(@PathVariable Long id, @RequestBody UsersDivisions usersDivisions) {
        try {
            UsersDivisions updatedUsersDivisions = usersDivisionsService.updateUsersDivisions(id, usersDivisions);
            return ResponseEntity.ok(updatedUsersDivisions);
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUsersDivisions(@PathVariable Long id) {
        usersDivisionsService.deleteUsersDivisions(id);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/user/{userId}/division/{divisionCode}")
    public ResponseEntity<Void> deleteUsersDivisionsByUserAndDivision(
            @PathVariable Long userId, @PathVariable String divisionCode) {
        usersDivisionsService.deleteUsersDivisionsByUserAndDivision(userId, divisionCode);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/status/{status}")
    public ResponseEntity<List<UsersDivisions>> getUsersDivisionsByStatus(@PathVariable String status) {
        List<UsersDivisions> usersDivisions = usersDivisionsService.getUsersDivisionsByStatus(status);
        return ResponseEntity.ok(usersDivisions);
    }
}